555B - Case of Fugitive - CodeForces Solution


data structures greedy sortings *2000

Please click on ads to support us..

Python Code:

from heapq import heappop, heappush
import os, sys
from io import BytesIO, IOBase

class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, 8192))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, 8192))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)

class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")

sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)

input = lambda: sys.stdin.readline().strip()
ints = lambda: list(map(int, input().split()))
Int = lambda: int(input())

def queryInteractive(a, b, c):
    print('? {} {} {}'.format(a, b, c))
    sys.stdout.flush()
    return int(input())

def answerInteractive(x1, x2):
    print('! {} {}'.format(x1, x2))
    sys.stdout.flush()

inf = float('inf')
from types import GeneratorType

def bootstrap(f, stack=[]):
    def wrappedfunc(*args, **kwargs):
        if stack:
            return f(*args, **kwargs)
        else:
            to = f(*args, **kwargs)
            while True:
                if type(to) is GeneratorType:
                    stack.append(to)
                    to = next(to)
                else:
                    stack.pop()
                    if not stack:
                        break
                    to = stack[-1].send(to)
            return to
    return wrappedfunc

'''
输入 n(2≤n≤2e5) 和 m(1≤m≤2e5),然后输入 n 行,每行有两个数,表示一个闭区间(1≤L≤R≤1e18),
然后输入一个长为 m 的数组 a (1≤a[i]≤1e18)。
输入保证区间之间没有交集,且上一个区间的右端点小于下一个区间的左端点。

你有 m 座桥,每座桥的长为 a[i],你需要选择 n-1 座桥连接所有相邻的区间(注意是相邻)。
要求桥的两个端点分别落在这两个闭区间内(这两个端点的差等于桥长)。

如果无法做到,输出 No;否则输出 Yes,然后按顺序输出这 n-1 座桥的编号(编号从 1 开始),
输出的第一座桥连接第一个区间和第二个区间,输出的第二座桥连接第二个区间和第三个区间,依此类推。
'''
def solve():
    n, m = list(map(int, input().split()))
        lr = [list(map(int,input().split())) for _ in range(n)]
    lrc = []
    for i in range(1, len(lr)):
        lrc.append((lr[i][0]-lr[i-1][1], lr[i][1]-lr[i-1][0]))
    lrc = [(x, i) for i, x in enumerate(lrc)]
    lrc.sort(key=lambda x:(x[0][0],x[0][1]))

    a = list(map(int, input().split()))
    idx = [(x, i) for i, x in enumerate(a)]
    idx.sort(key=lambda x:x[0])
    ret = [-1] * (n-1)
    if n-1 > m:
        print("No")
        return


        hp, j = [], 0
    for t,i in idx:
                while j < len(lrc) and lrc[j][0][0] <= t:
            heappush(hp, (lrc[j][0][1], lrc[j][1]))
            j += 1
        if hp:
            if hp[0][0] < t: break
            ret[heappop(hp)[1]] = i+1

    if hp or j != len(lrc):
        print("No")
        return

    print("Yes")
    print(" ".join(map(str, ret)))
solve()
    


Comments

Submit
0 Comments
More Questions

36. Valid Sudoku
557. Reverse Words in a String III
566. Reshape the Matrix
167. Two Sum II - Input array is sorted
387. First Unique Character in a String
383. Ransom Note
242. Valid Anagram
141. Linked List Cycle
21. Merge Two Sorted Lists
203. Remove Linked List Elements
733. Flood Fill
206. Reverse Linked List
83. Remove Duplicates from Sorted List
116. Populating Next Right Pointers in Each Node
145. Binary Tree Postorder Traversal
94. Binary Tree Inorder Traversal
101. Symmetric Tree
77. Combinations
46. Permutations
226. Invert Binary Tree
112. Path Sum
1556A - A Variety of Operations
136. Single Number
169. Majority Element
119. Pascal's Triangle II
409. Longest Palindrome
1574A - Regular Bracket Sequences
1574B - Combinatorics Homework
1567A - Domino Disaster
1593A - Elections